# -*- coding: UTF-8 -*-
"""
HaaS506 DTU - System Module
Version: v1.0.0
"""
import service_storage


def init():
    """Initialize system module"""
    mark_reboot_times()
    print("[SYSTEM] System module initialized")


# rewrite reboot()
# before reboot ,write kv last timestamp
def restart():
    """Reboot system"""
    print("[SYSTEM] Rebooting system...")
    service_storage.runtime_storage.write_last_timestamp()
    reboot()


def mark_reboot_times():
    """Mark reboot times"""
    # 初始化存储服务
    try:
        service_storage.runtime_storage.init_kv_storage()
        reboot_count = service_storage.runtime_storage.increment_reboot_count()
        last_timestamp = service_storage.runtime_storage.read_last_timestamp()
        print("[MAIN] System reboot #{} times, last reboot timestamp: {}".format(reboot_count, last_timestamp))
    except Exception as e:
        print("[MAIN] Storage service init failed: {}".format(e))
